<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./Ajax.js"></script>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
        }

        .fade-enter-active,
        .fade-leave-active {
            transition: opacity 0.5s;
        }

        .fade-enter,
        .fade-leave-to {
            opacity: 0;
        }

        #box {
            position: absolute;
            top: 0px;
            left: 0px;
            right: 0px;
            bottom: 0px;
            background: rgba(130, 126, 126, 0.45);
            display: none;
        }

        #box form {
            width: 600px;
            height: 400px;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0px 0px 20px 5px;
            display: flex;
            flex-direction: column;
            align-items: center;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
        }

        #box form h2 {
            height: 70px;
            text-align: center;
            line-height: 70px;
            width: 100%;
            background: #4169e1;
            cursor: move;
            /* 设置 h2的左上角 和右上角的 圆角 */
            border-radius: 10px 10px 0px 0px;
        }

        #box form p {
            width: 230px;
            margin-top: 10px;
        }

        #box form input,
        #box form select {
            height: 25px;
        }

        #box form button {
            width: 80px;
            height: 30px;
            background: royalblue;
            border: 1px solid #4169e1;
            color: #fff;
        }

        table {
            border: 1px solid red;
            width: 80%;
            margin: auto;
            /* 下面两句代码是合并单元格之间的距离 */
            border-spacing: 0px;
            border-collapse: collapse;
        }

        table td,
        table th {
            border: 1px solid red;
            text-align: center;
            height: 30px;
        }

        table th {
            background: orange;
        }
    </style>


</head>

<body>
    <div id="app">
        <button type="button" id="btn">添加数据</button>
        <div id="box">
            <form>
                <h2>学生信息录入</h2>
                <p>
                    <label>姓名：</label>
                    <input type="text" id="name" />
                </p>
                <p>
                    <label>年龄：</label>
                    <input type="text" id="age" />
                </p>
                <p>
                    <label>性别：</label>
                    <select id="sex">
                        <option value="男">男</option>
                        <option value="女">女</option>
                    </select>
                </p>
                <p>
                    <label>手机：</label>
                    <input type="text" id="tel" />
                </p>
                <p>
                    <button type="button" id="createData">创建信息</button>
                    <button type="button" id="close">关闭</button>
                </p>
            </form>
        </div>
        <table>
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>手机号</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <!-- <tr>
                    <td>sd</td>
                    <td>13</td>
                    <td>男</td>
                    <td>18878788989</td>
                    <td><button type="button">删除</button></td>
                </tr> -->
            </tbody>
        </table>
    </div>
    <script>
        let app = document.querySelector('#app');
        let btn = document.querySelector('#btn');
        let tbody = app.querySelector('tbody');
        let box = document.querySelector('#box');
        let clo = document.querySelector('#close');
        let createData = document.querySelector('#createData');
        let name = document.querySelector('#name');
        let age = document.querySelector('#age');
        let sex = document.querySelector('#sex');
        let tel = document.querySelector('#tel');


        // Ajax({
        //     url: '../5.6getData.php',
        //     success: function (res) {
        //         res = JSON.parse(res);
        //         Runder(res);
        //     }
        // });

        async function getDate() {
            let res = await  pAjax({
                url: '../5.6getData.php',
            });
            res = JSON.parse(res);
            Runder(res);
        }

        getDate();

        btn.onclick = function () {
            box.style.display = 'block';
        }
        createData.onclick = function () {
            pAjax({
                url: '../5.6addData.php',
                data: {
                    name: name.value,
                    age: age.value,
                    sex: sex.value,
                    tel: tel.value,
                }
            }).then(res => {
                res = JSON.parse(res);
                if (res.code) {
                    getDate();
                    clear();
                }
            });

        }
        clo.onclick = function () {
            clear();
        }
        tbody.onclick = (e) => {
            if (e.target.className == 'del') {
                let id = e.target.getAttribute('idx');
                pAjax({
                    url: '../5.6deleteData.php',
                    data: {
                        id //id==id因为同一个
                    }
                }).then(function (res) {
                    res = JSON.parse(res);
                    if (res.code) {
                        getDate();
                    }
                });
            }
        }


        function Runder(data) {
            let str = '';
            data.forEach(item => {
                str += `
                    <tr>
                    <td>${item.name}</td>
                    <td>${item.age}</td>
                    <td>${item.sex}</td>
                    <td>${item.tel}</td>
                    <td><button type="button" class='del' idx="${item.id}">删除</button></td>
                    </tr> 
                    `;
            });
            tbody.innerHTML = str;
        }

        function clear() {
            name.value = age.value = sex.value = tel.value = '';
            box.style.display = 'none';
        }
    </script>
</body>

</html>